package com.offcn.servlet;

import com.alibaba.fastjson.JSON;
import com.offcn.entity.Course;
import com.offcn.service.Impl.PageServiceImpl;
import com.offcn.service.PageSerivce;
import com.offcn.utils.PageTool;
import com.offcn.utils.ResultBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.RescaleOp;
import java.io.IOException;
import java.util.List;

@WebServlet("/page")
public class PageServlet extends BaseServlet {
    PageSerivce service = new PageServiceImpl();
    ResultBean bean = null;

    //    根据课程类型查询课程信息
    protected void findByCourseType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int courseType = Integer.parseInt(request.getParameter("courseType"));
        String currentPage = request.getParameter("currentPage");
        String search = request.getParameter("search");
        int totalCount = service.getTotalCount(courseType, search);
        PageTool pageTool = new PageTool(totalCount, currentPage, 6);
        List<Course> list = service.findByCourseType(courseType, pageTool, search);
        if (list != null && list.size() > 0) {
            pageTool.setList(list);
            bean = new ResultBean(200, "查询成功", pageTool);
        } else {
            bean = new ResultBean(500, "查询失败", null);
        }
        response.getWriter().write(JSON.toJSONString(bean));
    }
}
